package trastienda.web;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import trastienda.excepcion.DAOExcepcion;
import trastienda.modelo.Categoria;
import trastienda.service.CategoriaService;


@Controller
public class CategoriaController {

	@Autowired
	private CategoriaService categoriaService;
		
	public void setCategoriaService(CategoriaService categoriaService) {
		this.categoriaService = categoriaService;
	}
		
	@RequestMapping(value = "/categoria_ingreso", method = RequestMethod.POST)	
	public ModelAndView categoria_ingreso(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		String a = request.getParameter("nombre");
		String b = request.getParameter("descripcion");
		
		try {
			categoriaService.insertar(a, b);
		} catch (DAOExcepcion e){
			return new ModelAndView("/error", "mensaje", "categoria_ingreso error");
		}
		return new ModelAndView("redirect:categoria_listado.html", "mensaje", "categoria_ingreso correcto");
	}

	@RequestMapping(value = "/categoria_listado", method = RequestMethod.GET)
	public ModelAndView categoria_listado(HttpServletRequest request, HttpServletResponse response) {
			
		Collection<Categoria> listaTodos = null;
		try {
		listaTodos = categoriaService.listarTodos();
		} catch (DAOExcepcion e){
			return new ModelAndView("/error", "mensaje", "categoria_listado error");
		}
		return new ModelAndView("admin/categoria_listar", "categorias_li", listaTodos);
	}

	@RequestMapping(value = "/categoria_preedicion", method = RequestMethod.GET)
	protected ModelAndView categoria_preedicion(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Categoria c = new Categoria();
		c = categoriaService.obtenerPorId(Integer.valueOf(request.getParameter("idcategoria")));

		return new ModelAndView("admin/categoria_editar", "categorias_ed", c);
	}
	
	@RequestMapping(value = "/categoria_preingreso", method = RequestMethod.POST)
	protected ModelAndView categoria_preingreso(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
					
		return new ModelAndView("admin/categoria_ingresar", "mensaje", "categoria_preingreso correcto");
	}
	
	@RequestMapping(value = "/categoria_edicion", method = RequestMethod.POST)
	protected ModelAndView categoria_edicion(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
						
		int a = Integer.valueOf(request.getParameter("cidCategoria"));
		String b = request.getParameter("cnombre");
		String c = request.getParameter("cdescripcion");
		
		try {
			categoriaService.actualizar(a, b, c);
		} catch (DAOExcepcion e){
			return new ModelAndView("/error", "mensaje", "categoria_edicion error");
		}
		return new ModelAndView("redirect:categoria_listado.html", "mensaje", "categoria_ingreso correcto");
	}
		
	@RequestMapping(value = "/categoria_eliminacion", method = RequestMethod.GET)
	protected ModelAndView categoria_eliminacion(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
						
		int a = Integer.valueOf(request.getParameter("idcategoria"));
		
		try {
			categoriaService.eliminarPorId(a);
		} catch (DAOExcepcion e){
			return new ModelAndView("/error", "mensaje", "categoria_eliminacion error");
		}
		return new ModelAndView("redirect:categoria_listado.html", "mensaje", "categoria_eliminacion correcto");
	}
	
}
